<!--Copyright 2022 The HuggingFace Team. All rights reserved.

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
specific language governing permissions and limitations under the License.
-->

# The Tasks Manager

Exporting a model from one framework to some format (also called backend here) involves specifying inputs and outputs information that the export function needs. The way `optimum.exporters` is structured for each backend is as follows:
- Configuration classes containing the information for each model to perform the export.
- Exporting functions using the proper configuration for the model to export.

The role of the [`~optimum.exporters.tasks.TasksManager`] is to be the main entry-point to load a model given a name and a task, and to get the proper configuration for a given (architecture, backend) couple. That way, there is a centralized place to register the `task -> model class` and `(architecture, backend) -> configuration` mappings. This allows the export functions to use this, and to rely on the various checks it provides.

## Task names

The tasks supported might depend on the backend, but here are the mappings between a task name and the auto class for both PyTorch and TensorFlow.

<Tip>

It is possible to know which tasks are supported for a model for a given backend, by doing:

```python
>>> from optimum.exporters.tasks import TasksManager

>>> model_type = "distilbert"
>>> # For instance, for the ONNX export.
>>> backend = "onnx"
>>> distilbert_tasks = list(TasksManager.get_supported_tasks_for_model_type(model_type, backend).keys())

>>> print(distilbert_tasks)
['default', 'fill-mask', 'text-classification', 'multiple-choice', 'token-classification', 'question-answering']
```

</Tip>

### PyTorch

| Task                                 | Auto Class                           |
|--------------------------------------|--------------------------------------|
| `text-generation`, `text-generation-with-past`   | `AutoModelForCausalLM`               |
| `feature-extraction`, `feature-extraction-with-past`       | `AutoModel`                          |
| `fill-mask`                          | `AutoModelForMaskedLM`               |
| `question-answering`                 | `AutoModelForQuestionAnswering`      |
| `text2text-generation`, `text2text-generation-with-past` | `AutoModelForSeq2SeqLM`              |
| `text-classification`            | `AutoModelForSequenceClassification` |
| `token-classification`               | `AutoModelForTokenClassification`    |
| `multiple-choice`                    | `AutoModelForMultipleChoice`         |
| `image-classification`               | `AutoModelForImageClassification`    |
| `object-detection`                   | `AutoModelForObjectDetection`        |
| `image-segmentation`                 | `AutoModelForImageSegmentation`      |
| `masked-im`                          | `AutoModelForMaskedImageModeling`    |
| `semantic-segmentation`              | `AutoModelForSemanticSegmentation`   |
| `automatic-speech-recognition`                      | `AutoModelForSpeechSeq2Seq`          |

### TensorFlow

| Task                                 | Auto Class                             |
|--------------------------------------|----------------------------------------|
| `text-generation`, `text-generation-with-past`   | `TFAutoModelForCausalLM`               |
| `default`, `default-with-past`       | `TFAutoModel`                          |
| `fill-mask`                          | `TFAutoModelForMaskedLM`               |
| `question-answering`                 | `TFAutoModelForQuestionAnswering`      |
| `text2text-generation`, `text2text-generation-with-past` | `TFAutoModelForSeq2SeqLM`              |
| `text-classification`            | `TFAutoModelForSequenceClassification` |
| `token-classification`               | `TFAutoModelForTokenClassification`    |
| `multiple-choice`                    | `TFAutoModelForMultipleChoice`         |
| `semantic-segmentation`              | `TFAutoModelForSemanticSegmentation`   |


## Reference

[[autodoc]] exporters.tasks.TasksManager
